import {getOrderDetail, orderPay} from '../../api/order.js';
import Util from '../../utils/util.js';

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      order_id: options.order_id || '',
      type: options.type || '',
      old: options.old || ''
    })

    const that = this
    wx.showLoading({
      title: '正在加载',
    })
    getOrderDetail(this.data.order_id)
      .then(res => {
        let order = res.data.order || []
        if (order.length == 0) {
          return app.Tips({title: "订单不存在"}, {tab: 3})
        } else if (order.status == 40) {
          return app.Tips({title: "订单已支付"}, {tab: 3})
        }

        that.setData({order: order})
        wx.hideLoading();
      }).catch(errmsg => {
      app.Tips({title: errmsg}, {tab: 3})
    });

    this.onLoadTabbar();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  payOrder(e) {
    const key = e.currentTarget.dataset.key,
      order = this.data.order,
      that = this

    const type = order.type,
      total_price = order.total_price,
      city = wx.getStorageSync('current_city')

    if (total_price == 0 && (type == 1 || type == 5 || type == 6 || type == 9)) {
      Util.showModelTip("当前账户抵扣，支付金额为0，确认后直接操作成功，确定吗？", "")
        .then(res => {
          orderDikou({order_id: order.out_trade_no})
            .then(res => {
              wx.setStorageSync('is_refresh', 1)
              if (type == 1) {
                return app.Tips({title: "发帖成功"}, {tab: 5, url: "/pages/grouppush/index?city=" + city})
              } else if (type == 5 || type == 6) {
                return app.Tips({title: "解锁成功"}, {tab: 3})
              } else if (type == 9) {
                return app.Tips({title: "解锁成功"}, {tab: 5, url: "/pages/nearby/index"})
              } else {
                return app.Tips({title: "解锁成功"}, {tab: 3})
              }
            }).catch(errmsg => {
            app.Tips({title: errmsg})
          })
        })
    } else {
      orderPay({order_id: order.out_trade_no})
        .then(res => {
          let data = res.data

          wx.requestPayment({
            'timeStamp': data.timeStamp,
            'nonceStr': data.nonceStr,
            'package': data.package,
            'signType': 'MD5',
            'paySign': data.paySign,
            success: function (res) {
              wx.setStorageSync('is_refresh', 1)
              return app.Tips({title: "报名成功", icon: "success"}, {tab: 3})
            },
            fail: function (res) {
              return app.Tips({title: "取消成功", icon: "success"})
            }
          })

        }).catch(errmsg => {
        app.Tips({title: errmsg})
      })
    }

  },

  showTip(e) {
    const content = e.currentTarget.dataset.msg || ''
    if (!content) return "";
    wx.showModal({
      title: '温馨提示',
      content: content,
      showCancel: false,
      showConfirm: false,
      confirmText: '朕知道了',
      confirmColor: '#EF6243',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  onLoadTabbar: function () {//底部tabbar返回数据
    const that = this

    Util.checkIphone().then(res => {//检查是否是iphone x/11等

      that.setData({
        isIphoneX: res,
        bottom: app.globalData.footerPadding
      })

      wx.createSelectorQuery().in(that).select('.weui-footer')
        .boundingClientRect(function (rect) {
          if (!rect) return;
          that.setData({
            bottom: parseInt(rect.height) + 15
          })
        }).exec()
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})